//
//  XPMainWindowController.m
//  QQ
//
//  Created by nhope on 2017/4/18.
//  Copyright © 2017年 xiaopin. All rights reserved.
//

#import "XPMainWindowController.h"
#import "XPMessageSplitViewController.h"
#import "XPContactSplitViewController.h"
#import "XPMoreViewController.h"
#import "NSViewController+XP.h"

@interface XPMainWindowController ()

@property (weak) IBOutlet NSToolbarItem *selectedToolbarItem;
@property (nonatomic, strong) XPMessageSplitViewController *messageSplitViewController;
@property (nonatomic, strong) XPContactSplitViewController *contactSplitViewController;
@property (nonatomic, strong) XPMoreViewController *moreViewController;

@end

@implementation XPMainWindowController

#pragma mark - Lifecycle

- (void)windowDidLoad {
    [super windowDidLoad];
    [self.window setTitleVisibility:NSWindowTitleHidden];
    _messageSplitViewController = [XPMessageSplitViewController instantiateControllerFromMainStoryboard];
    self.window.contentViewController = _messageSplitViewController;
}

#pragma mark - Action

- (IBAction)messageToolbarItemAction:(NSToolbarItem *)sender {
    if ([self.window.contentViewController isMemberOfClass:[XPMessageSplitViewController class]]) {
        return;
    }
    [self cancelCurrentSelectedToolbarItem];
    _selectedToolbarItem = sender;
    sender.image = [NSImage imageNamed:@"menu-message-down"];
    if (nil == _messageSplitViewController) {
        _messageSplitViewController = [XPMessageSplitViewController instantiateControllerFromMainStoryboard];
    }
    _messageSplitViewController.view.frame = self.window.contentView.frame;
    self.window.contentViewController = _messageSplitViewController;
}

- (IBAction)contactToolbarItemAction:(NSToolbarItem *)sender {
    if ([self.window.contentViewController isMemberOfClass:[XPContactSplitViewController class]]) {
        return;
    }
    [self cancelCurrentSelectedToolbarItem];
    _selectedToolbarItem = sender;
    sender.image = [NSImage imageNamed:@"menu-contact-down"];
    if (nil == _contactSplitViewController) {
        _contactSplitViewController = [XPContactSplitViewController instantiateControllerFromMainStoryboard];
    }
    _contactSplitViewController.view.frame = self.window.contentView.frame;
    self.window.contentViewController = _contactSplitViewController;
}

- (IBAction)moreToolbarItemAction:(NSToolbarItem *)sender {
    if ([self.window.contentViewController isMemberOfClass:[XPMoreViewController class]]) {
        return;
    }
    [self cancelCurrentSelectedToolbarItem];
    _selectedToolbarItem = sender;
    sender.image = [NSImage imageNamed:@"menu-more-down"];
    if (nil == _moreViewController) {
        _moreViewController = [XPMoreViewController instantiateControllerFromMainStoryboard];
    }
    _moreViewController.view.frame = self.window.contentView.frame;
    self.window.contentViewController = _moreViewController;
}

#pragma mark - Private

/// 取消当前选中的item图片
- (void)cancelCurrentSelectedToolbarItem {
    NSString *icon = nil;
    switch (_selectedToolbarItem.tag) {
        case XPToolbarItemMessageTag:
            icon = @"menu-message-normal";
            break;
        case XPToolbarItemContactTag:
            icon = @"menu-contact-normal";
            break;
        case XPToolbarItemMoreTag:
            icon = @"menu-more-normal";
            break;
        case XPToolbarItemAvatarTag:
        default:
            return;
            break;
    }
    _selectedToolbarItem.image = [NSImage imageNamed:icon];
}

@end
